var express = require('express');
var router = express.Router();
var path = require("path");

// 获取验证模块
var authorization = require(path.join(process.cwd(), "/modules/authorization"));

// 角色管理模块
var memberServ = authorization.getService("MemberService");

// 获取成员列表
router.get("/",
    // 验证参数
    function(req,res,next) {
        // 参数验证
        console.log(req.query)
        if(!req.query.pagenum || req.query.pagenum <= 0) return res.sendResult(null,400,"pagenum 参数错误");
        if(!req.query.pagesize || req.query.pagesize <= 0) return res.sendResult(null,400,"pagesize 参数错误");
        next();
    },
    // 处理业务逻辑
    function(req,res,next) {
        memberServ.getAllMembers(
            {
                "query":req.query.query,
                "pagenum":req.query.pagenum,
                "pagesize":req.query.pagesize,
                "type":req.query.type,

            },
            function(err,result){
                if(err) return res.sendResult(null,400,err);
                res.sendResult(result,200,"获取成员列表成功");
            }
        )(req,res,next);

    }
);
// 通过id获取成员信息
router.get("/:id",
    // 验证参数
    function(req,res,next) {
        // 参数验证
        if(!req.params.id ) return res.sendResult(null,400,"请求信息出错");
        next();
    },
    // 处理业务逻辑
    function(req,res,next) {
        memberServ.getMemberById(req.params.id
            ,
            function(err,result){
                if(err) return res.sendResult(null,400,err);
                res.sendResult(result,200,"获取成员信息成功");
            }
        )(req,res,next);

    }
);

// 添加成员
router.put("/",
    // 参数验证
    function (req, res, next) {

        if (!req.body.user_id) return res.sendResult(null, 400, "学号不能为空");
        if (!req.body.user_name) return res.sendResult(null, 400, "姓名不能为空");
        next();
    },
    // 处理业务逻辑
    function (req, res, next) {

        memberServ.addMember({
            "user_id": req.body.user_id,
            "user_name": req.body.user_name,
            "sex": req.body.sex,
            "class": req.body.class,
            "type": req.body.type,
            "lib": req.body.lib,
            "teacher": req.body.teacher,
            "tel": req.body.tel,
            "addr": req.body.addr,
            "qq": req.body.qq,
            "status": 1,
            "remark": req.body.remark
        }, function (err, role) {
            if (err) return res.sendResult(null, 400, err);
            res.sendResult(role, 200, "创建成功");
        })(req, res, next);
    }
);
// 删除成员
router.delete("/:id",
    // 参数验证
    function (req, res, next) {
        if(!req.params.id) return res.sendResult(null,400,"成员ID不能为空");
        next();
    },
    // 处理业务逻辑
    function (req, res, next) {
        memberServ.deleteMember(req.params.id, function (err, role) {
            if (err) return res.sendResult(null, 400, err);
            res.sendResult(role, 200, "删除成功");
        })(req, res, next);
    }
);

//更新成员
router.put("/:id/update",
    // 参数验证
    function (req, res, next) {
        console.log(req.body);
        if (!req.body.user_id) return res.sendResult(null, 400, "学号不能为空");
        if (!req.body.user_name) return res.sendResult(null, 400, "姓名不能为空");
        next();
    },
    // 处理业务逻辑
    function (req, res, next) {
        console.log(req.body);
        console.log(req.params.id);
        memberServ.updateMember( req.params.id,req.body, function (err, role) {
            if (err) return res.sendResult(null, 400, err);
            res.sendResult(role, 200, "更新成功");
        })(req, res, next);
    }
);


module.exports = router;
